<?php
// +----------------------------------------------------------------------
// | 车钉钉 [ WE CAN DO IT JUST THINK IT ]
// +----------------------------------------------------------------------
// | Copyright (c) 2017 http://www.twothink.cn All rights reserved.
// +----------------------------------------------------------------------
// | Author: YagamiZN
// +----------------------------------------------------------------------

namespace app\home\controller;

/**
 * 登陆注册控制器
 */
class Login extends Home{
    /**
     * 构造方法，初始化站点信息
     */
    protected function _initialize(){
        parent::_initialize();
    }
    //用户注册ajax
    public function reg(){
        $tel=input('post.tel');
        $pwd=input('post.pwd');
        $repwd=input('post.repwd');
        $vcode=input('post.vcode/d');
        if(empty($tel)||!preg_match(ZZ_PHONE, $tel)){
            return ['code'=>300,'msg'=>'手机号格式错误'];
        }elseif (empty($pwd)||!preg_match(ZZ_PASSWORD, $pwd)){
            return ['code'=>300,'msg'=>'密码不合法'];
        }elseif ($pwd!==$repwd){
            return ['code'=>300,'msg'=>'两次密码输入不一致'];
        }elseif ($vcode!=1234){
            return ['code'=>300,'msg'=>'验证码错误'];
        }
        $isTelephone=json_decode($this->isTelephone(),true);
        if($isTelephone['code']==200){
            $user=model('User');
            $pwdkey=mt_rand(0,9999);
            $data=['telephone'=>$tel,'pwd'=>$pwd.$pwdkey,'pwdkey'=>$pwdkey];
            $user->save($data);
            $uid=$user->uid;
            $dir = STATIC_PATH . "/member/" . $uid . "/";
            file_exists($dir) || (mkdir($dir, 0777, true) && chmod($dir, 0777));
            return ['code'=>200,'msg'=>'注册成功'];
        }else{
            return ['code'=>300,'msg'=>'该手机号已经注册'];
        }
    }
    //检测帐号是否已经被注册ajax
    public function isTelephone(){
        $tel=input('param.tel');
        if(empty($tel)||!preg_match(ZZ_PHONE, $tel)){
            return json_encode(['code'=>300,'msg'=>'手机号格式错误']);
        }
        $isOK=db('user')->where('telephone',$tel)->find();
        if(empty($isOK)){
            return json_encode(['code'=>200,'msg'=>'该手机号可以注册']);
        }else{
            return json_encode(['code'=>300,'msg'=>'该手机号已经注册']);
        }
    }
    //用户登陆ajax
    public function login(){
        $tel=input('post.tel');
        $pwd=input('post.pwd');
        if(empty($tel)||!preg_match(ZZ_PHONE, $tel)){
            return ['code'=>300,'msg'=>'手机号格式错误'];
        }elseif (empty($pwd)||!preg_match(ZZ_PASSWORD, $pwd)){
            return ['code'=>300,'msg'=>'密码不合法'];
        }
        $isCheck=json_decode($this->loginCheck($tel,$pwd),true);
        if($isCheck['code']==200){
            return ['code'=>200,'msg'=>'登陆成功','url'=>$_SERVER['HTTP_REFERER']];
        }else{
            return ['code'=>300,'msg'=>'密码错误'];
        }
    }
    //用户登陆认证
    public function loginCheck($tel,$pwd){
        $user=model('User');
        $userinfo=$user->where('telephone',$tel)->find()->toArray();
        if(empty($userinfo)){
            return json_encode(['code'=>300,'msg'=>'该手机未注册']);
        }else{
            $md5Pwd=md5($pwd.$userinfo['pwdkey']);
            if($md5Pwd===$userinfo['pwd']){
                session('uid', $userinfo['uid']);
                session('tel', $userinfo['telephone']);
                $user->update('',['uid'=>$userinfo['uid']]);
                return json_encode(['code'=>200,'msg'=>'登陆成功']);
            }else{
                return json_encode(['code'=>300,'msg'=>'密码错误']);
            }
        }
    }
    //退出
    public function loginOut(){
        session(null);
        $this->redirect('index');
    }
    
    /* 空操作，用于输出404页面 */
    public function _empty(){
        $this->redirect('Index/index');
    }
}
